package com.shitou.shitouorder.controller;


import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

import com.shitou.shitoucommons.annotation.CheckUserLogin;
import com.shitou.shitoucommons.utils.AllSaltEnum;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoucommons.vo.RefundVo;
import com.shitou.shitouorder.entity.OrderEntity;
import com.shitou.shitouorder.vo.DeleteOrderVo;
import com.shitou.shitouorder.vo.GetMemberOrderVo;
import com.shitou.shitouorder.vo.OrderVo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.shitou.shitouorder.service.OrderService;
import com.shitou.shitoucommons.utils.RespBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 订单
 *
 * @author shitouren
 * @email 3076767823@qq.com
 * @date 2023-03-21 16:29:51
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("shitouorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RespBean list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return RespBean.success().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public RespBean info(@PathVariable("id") Long id){
        OrderEntity order = orderService.getById(id);

        return RespBean.success().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RespBean save(@RequestBody OrderEntity order){
        orderService.save(order);

        return RespBean.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RespBean update(@RequestBody OrderEntity order){
        orderService.updateById(order);

        return RespBean.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RespBean delete(@RequestBody Long[] ids){
        orderService.removeByIds(Arrays.asList(ids));

        return RespBean.success();
    }




    /**
     * 创建订单
     * @param orderVo 映射类
     * @see OrderVo
     * @return RespBean
     */
    @ApiOperation(value = "创建订单，并设置最长支付时间，需要登录校验")
    @CheckUserLogin
    @PostMapping("/createOrder")
    public RespBean createOrder(@RequestBody OrderVo orderVo, HttpServletRequest request, HttpServletResponse response){
        return orderService.createOrder(orderVo, request, response);
    }

//    @RequestMapping("/test")
//    public RespBean test(@RequestBody OrderVo orderVo, HttpServletRequest request, HttpServletResponse response){
//        return orderService.test(orderVo, request, response);
//    }
    @RequestMapping("/test/createOrder")
    public RespBean test(@RequestParam Long memberId,@RequestParam long[] spuIds, @RequestParam Double price, HttpServletRequest request, HttpServletResponse response){
        OrderVo orderVo = new OrderVo();
        orderVo.setMemberId(memberId);
        orderVo.setSpuIds(spuIds);
        orderVo.setPrice(BigDecimal.valueOf(price));
        System.out.println(orderVo);
        return orderService.test(orderVo, request, response);
    }

    public static void main(String[] args) {
        String s = new Sha256Hash(String.valueOf(1L), AllSaltEnum.SALT.getSalt()).toString();

        System.out.println(s);
    }

    /**
     * 通过状态获取用户的订单
     * @param memberOrderVo 映射类
     * @see GetMemberOrderVo
     * @return RespBean
     */
    @ApiOperation(value = "获得当前登录用户的订单信息，需要登录校验")
    @CheckUserLogin
    @PostMapping("/getMemberOrder")
    public RespBean getMemberOrder(@Validated @RequestBody GetMemberOrderVo memberOrderVo, HttpServletRequest request, HttpServletResponse response){
        return orderService.getMemberOrder(memberOrderVo, request, response);
    }


    /**
     * 获得该用户的分页数据,通过状态和参数
     * @param memberOrderVo 映射类
     * @see GetMemberOrderVo
     * @param params 分夜查询的参数
     * @return RespBean
     */
    @Deprecated
    @ApiOperation(value = "该接口已经弃用")
    @CheckUserLogin
    @PostMapping("/getMemberOrderPage")
    public RespBean getMemberOrderPage(@Validated @RequestBody GetMemberOrderVo memberOrderVo, Map<String, Object> params){
        return orderService.getMemberOrderPage(memberOrderVo, params);
    }


    /**
     * 删除用户的订单信息
     * @param deleteOrderVo 映射类提
     * @see DeleteOrderVo
     * @return RespBean
     */
    @ApiOperation(value = "删除单个订单新，通过订单id，采用伪删除，需要登录校验")
    @CheckUserLogin
    @PostMapping("/deleteOrder")
    public RespBean deleteOrder(@Validated @RequestBody DeleteOrderVo deleteOrderVo){
        return orderService.deleteOrder(deleteOrderVo);
    }


    /**
     * 订单自动收货
     * @param orderSn 订单号
     * @return RespBean
     */
    @ApiOperation(value = "用户进行手动收获时，调用，通过订单号进行收货，需要登录校验")
    @CheckUserLogin
    @PostMapping("/confirm")
    public RespBean confirm(@RequestParam String orderSn){
        return orderService.confirm(orderSn);
    }


    /**
     * 获得订单的详细信息
     * @param orderSn 订单号
     * @param spuId 商品id
     * @param memberId 用户id
     * @return RespBean
     */
    @ApiOperation(value = "通过订单号获得单个订单的详细信息, 需要登录校验")
    @CheckUserLogin
    @PostMapping("/orderInfo")
    public RespBean orderInfo(@RequestParam String orderSn, @RequestParam long spuId, @RequestParam long memberId){
        return orderService.getOrderInfo(orderSn, spuId, memberId);
    }


    /**
     * 申请退款请求
     * @param memberId 买家id
     * @param orderSn 订单号
     * @param orderItemId 该订单号下所对应的商品所组成的
     * @return
     */
    @ApiOperation(value = "通过订单号获得单个订单的详细信息, 需要登录校验")
    @CheckUserLogin
    @PostMapping("/refund")
    public RespBean refund(@RequestParam Long memberId, @RequestParam String orderSn, @RequestParam Long orderItemId){
        return orderService.refund(memberId, orderSn, orderItemId);
    }


    /**
     * 对退款请求的操作，同意退款或者不不同意退款
     * @param memberId
     * @param orderSn
     * @param orderItemId
     * @return
     */
    @ApiOperation(value = "对退款请求的操作，同意退款或者不不同意退款")
    @CheckUserLogin
    @PostMapping("/refundOption")
    public RespBean refundOption(@RequestBody RefundVo refundVo){
        return orderService.refundOption(refundVo);
    }


    /**
     * 通过会员id获取相应的订单信息
     * @param params
     * @param memberId
     * @return
     */
    @GetMapping("/queryPageBymemberId")
    @ApiOperation(value = "通过用户id获取改用会的所有订单信息")
    public RespBean queryPageByMemberId(@RequestParam Map<String, Object> params, @RequestParam Long memberId){
        return orderService.queryPageByMemberId(params, memberId);
    }


}
